package com.kust.smarthome.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kust.smarthome.web.model.DeviceState;
import com.kust.smarthome.web.model.Lamp;
import com.kust.smarthome.web.model.User;
import com.kust.smarthome.web.service.IDeviceStateService;
import com.kust.smarthome.web.service.ILampService;
import com.kust.smarthome.web.service.IUserService;
import com.kust.smarthome.web.vo.DeviceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author iAuzre
 * @since 2021-06-28
 */

@RestController
@RequestMapping("/lamp")
@Api(value = "灯相关操作", tags = {"device-tag"})
public class LampController{

    @Resource
    private ILampService lampService;

    @Resource
    private IDeviceStateService deviceStateService;

    @Resource
    private IUserService userService;

    /**
     * 通过用户信息获取对应设备列表
     * @param principal Security框架中获取登录用户的详细信息
     * @return 对应用户的灯设备表
     */
    @GetMapping("")
    @ApiOperation(value = "查灯帘信息", notes = "根据用户信息查询对应用户灯信息")
    public List<Lamp> list(Principal principal) {
        User u = userService.getByUsername(principal.getName());
        return lampService.getByUserId(u.getId());
    }

    @GetMapping("/getById")
    public Lamp getById(Integer id) {
        return lampService.getById(id);
    }

//    /**
//     * 客户端请求一次开关灯,传给后端id,若该id对应的灯的状态为1(开)则将状态变为0,反之变为1
//     * 同时,在状态表中插入对应状态
//     * @param id curtain.getId()
//     * @return 返回所有的灯属性,前端js代码成功收到响应之后将vue中的数据重新绑定即可
//     */
//    @GetMapping("/operation")
//    @Transactional
//    @ApiOperation(value = "开关灯操作", notes = "用户提供灯id进行开关灯操作")
//    public List<Lamp> operationById(Principal principal, Integer id) {
//        Lamp lamp = lampService.getById(id);//
//        Integer state = 1;
//        if (lamp.getState() == 1) {
//            lamp.setState(0);
//            state = 0;
//        } else if (lamp.getState() == 0) {
//            lamp.setState(1);
//            state = 1;
//        }
//
//        lampService.updateById(lamp);
//        DeviceState deviceState = new DeviceState()
//                .setDeviceId(id)
//                .setTypeId(2)
//                .setIsUpdate(0);
//        //更改状态通知
//        QueryWrapper<DeviceState> queryD = new QueryWrapper<>();
//        queryD.eq("device_id",id);
//        queryD.eq("type_id",1);
//        deviceStateService.update(deviceState,queryD);
//
//        User u = userService.getByUsername(principal.getName());
//        return lampService.getByUserId(u.getId());
//    }
//


    @GetMapping("/update")
    @Transactional
    @ApiOperation(value = "开关灯操作", notes = "提供灯上传状态")
    public List<DeviceState> updateById(Integer id, Integer state,String content) {
        Lamp lamp = lampService.getById(id);
        lamp.setState(state);
        lamp.setContent(content);
        lampService.updateById(lamp);
        System.out.println("##################################################");
        System.out.println(state);
        System.out.println("##################################################");
        return lampService.findOperationList(id,1);


//        DeviceState deviceState = new DeviceState()
//                .setDeviceId(id)
//                .setTypeId(2)
//                .setIsUpdate(0);
//        //更改状态通知
//        QueryWrapper<DeviceState> queryD = new QueryWrapper<>();
//        queryD.eq("device_id",id);
//        queryD.eq("type_id",1);
//        deviceStateService.update(deviceState,queryD);
//
//        QueryWrapper<DeviceState> queryD = new QueryWrapper<>();

    }

    @GetMapping("/done")
    @ApiOperation(value = "改变操作状态", notes = "前端提供id")
    public boolean changeUpdate(Integer id){
        lampService.setupdate(id);
        return true;
    }



    /**
     * 网页端用户添加灯,方法中初始化一个灯对象并插入到数据表中
     * 若对灯数量有限制,前端可以添加一个判定条件,是否执行异步请求,同时后端也会做一次判定
     * 最大数量先定为3
     * @return 返回插入之后的灯列表
     */
    @GetMapping("/add")
    @Transactional
    @ApiOperation(value = "添加灯", notes = "用户提供id进行添加灯操作")
    public List<Lamp> addCurtain(Principal principal, DeviceVo deviceVo){

        User u = userService.getByUsername(principal.getName());

        List<Lamp> list = lampService.getByUserId(u.getId());

        LocalDateTime now = LocalDateTime.now();

        if (list.size() < 3) {
            Lamp lamp = new Lamp()
                    .setId(deviceVo.getId())
                    .setUrl("")
                    .setName(deviceVo.getName())
                    .setState(0)
                    .setCreated(now)
                    .setRoomId(deviceVo.getRoomId())
                    .setUserId(u.getId())
                    .setUserId(deviceVo.getRoomId());
            lampService.insert(lamp);
            list.add(lamp);

            DeviceState deviceState = new DeviceState()
                    .setDeviceId(deviceVo.getId())
                    .setTypeId(1)
                    .setIsUpdate(0);
            //插入状态变更通知
            deviceStateService.insert(deviceState);
        }
        return list;
    }

    /**
     * 删除一个灯对象操作
     * 同时要删除其所对应的状态表
     * @param id 由客户端传入一个id
     * @return 当后端处理完删除操作之后返回当前数据库中的灯对象列表
     */
    @GetMapping("/deleteById")
    @Transactional
    @ApiOperation(value = "删除灯", notes = "用户提供id进行删除灯操作")
    public List<Lamp> deleteById(Principal principal, Integer id) {
        Lamp lamp = lampService.getById(id);
        lamp.setUserId(-1);
        lampService.updateById(lamp);
        QueryWrapper<DeviceState> query = new QueryWrapper<>();
        query.eq("device_id",lamp.getId());
        query.eq("type_id",1);
        deviceStateService.remove(query);

        User u = userService.getByUsername(principal.getName());
        return lampService.getByUserId(u.getId());
    }

}
